<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Noticia extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function index()
	{

		$this->load->model('admin/noticia_model');
		
		$query = $this->db->get('noticia');
		$data['noticias'] = $query->result();
	
		$data = array(
            'title_for_layout' => 'Administra&ccedil;&atilde;o | Not&iacute;cia',
            'title_for_page'   => 'Listagem de Not&iacute;cia',
            'noticias'         => $query->result(),
            'content_layout'   => 'admin/modulos/noticia/noticia'
        );
        $this->load->view('admin/layout_main', $data);
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function cadastro()
	{
	    $data['title_for_layout'] = 'Administra&ccedil;&atilde;o';
		$data['content_layout']   = 'admin/modulos/noticia/noticia_add';
		$this->parser->parse('admin/layout_main', $data);
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function cadastroOK()
	{
		$config = array(
			array(
                	'field' => 'titulo',
                	'label' => 'Titulo',
                	'rules' => 'required|min_length[3]|trim'
                ),
                array(
                	'field' => 'subtitulo',
                	'label' => 'Subtitulo',
                	'rules' => 'required|min_length[3]|trim'
                ),
                array(
                	'field' => 'descricao',
                	'label' => 'Descricao',
                	'rules' => 'required|min_length[100]|max_length[400]|htmlspecialchars'
                ),
                array(
                	'field' => 'autor',
                	'label' => 'Autor',
                	'rules' => 'required|min_length[3]'
                )
       );
       
       $this->form_validation->set_rules($config);
       if ($this->form_validation->run() == FALSE)
       {
          $this->index();
       }
       else
       {
          $data['titulo']    = $this->input->post('titulo');
          $data['subtitulo'] = $this->input->post('subtitulo');
          $data['descricao'] = $this->input->post('descricao');
          $data['autor']     = $this->input->post('autor');

          $this->load->model('admin/noticia_model');
          if ($this->noticia_model->cadastrar($data))
          {
             $this->index();
          }
          else
          {
             echo 'Erro ao inserir a noticia';
          }
       }
	}

	/**
	 * Busca os dados de um registro único do sistema.
	 *
	 * @author Bruno Pulis  bruno.pulis[at]gmail.com
	 * @access public
	 * @param  $id int Chave primária do registro
	 */
	public function alterar($id) {
		$this->output->cache(60);
		$this->load->model('admin/noticia_model');
		$data = array(
            'title_for_layout' => 'Administra&ccedil;&atilde;o | Not&iacute;cia',
            'title_for_page'   => 'Editar Not&iacute;cia',
            'dados_categoria'  => $this->noticia_model->alterar($id),
            'content_layout'   => 'admin/noticia_alterar'
         );
         $this->parser->parse('admin/layout_main', $data);
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function gravar_alteracao()
	{
		$this->output->cache(60);
		$this->load->library('form_validation');
		$config = array(
			array(
                'field' => 'titulo',
                'label' => 'Titulo',
                'rules' => 'required|min_length[3]|trim'
            ),
            array(
                'field' => 'subtitulo',
                'label' => 'Subtitulo',
                'rules' => 'required|min_length[3]|trim'
            ),
            array(
                'field' => 'noticia',
                'label' => 'Noticia',
                'rules' => 'required|min_length[100]|max_length[400]|htmlspecialchars'
            ),
            array(
                'field' => 'autor',
                'label' => 'Autor',
                'rules' => 'required|min_length[3]'
            )
       );
      $this->form_validation->set_rules($config);

      if ($this->form_validation->run() == FALSE) {
         $this->alterar($this->input->post('id'));
      }
      else {
         $data['id']        = $this->input->post('id');
         $data['titulo']    = $this->input->post('titulo');
         $data['subtitulo'] = $this->input->post('subtitulo');
         $data['noticia']   = $this->input->post('noticia');
         $data['autor']     = $this->input->post('autor');
                 
         $this->load->model('admin/noticia_model');
         if ($this->noticia_model->gravar_alteracao($data)) {
             redirect(base_url() . 'admin/noticia/', 'refresh');
         } else {
             echo "Erro ao alterar notícia";
         }
      }
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function excluir($id) {
		$this->output->cache(60);
		$this->load->model('admin/noticia_model');
		if ($this->noticia_model->excluir($id)) {
			redirect('admin/noticia/', 'refresh');
		} else {
			echo "Erro ao excluir noticia";
		}
	}

}

/* End of file raca.php */
/* Location: ./application/controller/admin/raca.php */